#!/usr/bin/env bash

MAKE=${MAKE:-make}
source ../regression-tests/common

rm -f pdns*.pid pdns-gsqlite3.conf pdns.sqlite3 named.conf

sed '/directory/ { s@./zones@../regression-tests/zones@ }' ../regression-tests/named.conf > ./named.conf

cat >> ./named.conf << __EOF__
zone "."{
        type primary;
        file "../../regression-tests.rootzone/zones/ROOT";
};
__EOF__

cat > pdns-gsqlite3.conf << __EOF__
launch=gsqlite3
gsqlite3-database=pdns.sqlite3
gsqlite3-dnssec
module-dir=../regression-tests/modules
__EOF__

ARGS="--config-dir=. --config-name=gsqlite3"

port=5501
nameserver=localhost

sqlite3 pdns.sqlite3 < ../modules/gsqlite3backend/schema.sqlite3.sql
tosql gsqlite | sqlite3 pdns.sqlite3
echo ANALYZE\; | sqlite3 pdns.sqlite3

for zone in $(grep 'zone ' named.conf | cut -f2 -d\")
do
        $PDNSUTIL $ARGS set-nsec3 $zone "1 0 1 abcd" >&2
        $PDNSUTIL $ARGS add-zone-key $zone rsasha256 1024 zsk active >&2
done

$RUNWRAPPER $PDNS --daemon=no --local-port=$port --socket-dir=./          \
        $ARGS \
        --allow-axfr-ips=127.0.0.1 \
        --cache-ttl=60 --module-dir=../regression-tests/modules >&2 &

check_process

set +e

skipreasons='nsec3'

. ../regression-tests/tests/verify-dnssec-zone/command

kill $(cat pdns-gsqlite3.pid)
